package ph.com.gs3.formalistics.model.vo.parsers.json;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.model.vo.business.document.DocumentAction;
import ph.com.gs3.formalistics.model.vo.business.form.Form;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData.InvalidFormFieldException;
import ph.com.gs3.formalistics.model.vo.business.form.FormViewContentData;

public class FormJSONParser {

	public static final String TAG = FormJSONParser.class.getSimpleName();

	public static final Form createFromJSON(JSONObject raw) throws JSONException,
	        InvalidFormFieldException {

		Form form = new Form();

		form.setWebId(raw.getString("form_id"));
		form.setName(raw.getString("form_name"));

		form.setWorkflowId(raw.getString("workflow_id"));
		form.setWebTableName(raw.getString("form_table_name"));

		// Form Fields
		String mobileFieldsStringRaw = raw.getString("mobile_fields");
		JSONObject mobileFieldsRaw = new JSONObject(mobileFieldsStringRaw);
		JSONArray activeFieldsRaw = mobileFieldsRaw.getJSONArray("fields");

		List<FormViewContentData> activeContents = createFormContentsFromJSON(activeFieldsRaw);
		form.setActiveFormContents(activeContents);

		// Field Configuration
		String fieldsEnabledStringRaw = raw.getString("on_create_fields_enabled");
		String fieldsRequiredStringRaw = raw.getString("on_create_fields_required");
		String fieldsHiddenStringRaw = raw.getString("on_create_fields_hidden");

		JSONArray fieldsEnabledRaw;
		if (fieldsEnabledStringRaw != null && !"null".equals(fieldsEnabledStringRaw)) {
			fieldsEnabledRaw = new JSONArray(fieldsEnabledStringRaw);
		} else {
			fieldsEnabledRaw = new JSONArray();
		}

		JSONArray fieldsRequiredRaw;
		if (fieldsRequiredStringRaw != null && !"null".equals(fieldsRequiredStringRaw)) {
			fieldsRequiredRaw = new JSONArray(fieldsRequiredStringRaw);
		} else {
			fieldsRequiredRaw = new JSONArray();
		}

		JSONArray fieldsHiddenRaw;
		if (fieldsHiddenStringRaw != null && !"null".equals(fieldsHiddenStringRaw)) {
			fieldsHiddenRaw = new JSONArray(fieldsHiddenStringRaw);
		} else {
			fieldsHiddenRaw = new JSONArray();
		}

		List<String> fieldsEnabled = JSONParser.createStringListFromJSONArray(fieldsEnabledRaw);
		List<String> fieldsRequired = JSONParser.createStringListFromJSONArray(fieldsRequiredRaw);
		List<String> fieldsHidden = JSONParser.createStringListFromJSONArray(fieldsHiddenRaw);

		form.setOnCreateFieldsEnabled(fieldsEnabled);
		form.setOnCreateFieldsRequired(fieldsRequired);
		form.setOnCreateFieldsHidden(fieldsHidden);

		// Actions
		JSONArray actionsRaw = raw.getJSONArray("on_create_actions");
		List<DocumentAction> onCreateActions = DocumentActionJSONParser.createFromJSON(actionsRaw);

		form.setOnCreateActions(onCreateActions);

		return form;

	}

	public static final JSONArray createJSONArrayFromActions(List<DocumentAction> actions)
	        throws JSONException {

		JSONArray jsonArrayActions = new JSONArray();

		for (DocumentAction action : actions) {
			JSONObject json = new JSONObject();

			json.put("label", action.getLabel());
			json.put("action", action.getAction());

			jsonArrayActions.put(json);

		}

		return jsonArrayActions;
	}

	public static List<FormViewContentData> createFormContentsFromJSON(JSONArray raw)
	        throws InvalidFormFieldException, JSONException {

		List<FormViewContentData> contents = new ArrayList<FormViewContentData>();
		for (int i = 0; i < raw.length(); i++) {
			// Throws InvalidFormFieldException
			FormViewContentData formContent = FormViewContentDataJSONParser.createFromJSON(raw.getJSONObject(i));
			if (formContent != null) {
				contents.add(formContent);
			} else {
				throw new InvalidFormFieldException("Failed to create field from: "
				        + raw.getJSONObject(i).toString());
			}
		}

		return contents;

	}

	public static JSONArray createJSONFromFormContents(List<FormViewContentData> formContents)
	        throws JSONException {

		JSONArray fieldsJSONArray = new JSONArray();

		for (FormViewContentData formContent : formContents) {
			fieldsJSONArray.put(formContent.getRawJSON());
		}

		return fieldsJSONArray;

	}

}
